<?php


namespace App\Repositories\Policy\Eloquent;


use App\Repositories\Policy\Contracts\OrderInterface;
use Curl\CurlService\CurlService;

class OrderRepository implements OrderInterface
{
    private $http = 'https://flights.ws.ctrip.com/Flight.Order.SupplierOpenAPI/Api/';
    private $param;

    private $booking2;

    public function __construct()
    {

        $this->param = [
            'UserName'=> 'hb-wh',
            'UserPassword'=>md5('hb-wh#q2115708.')
        ];

        $this->booking2 = [
            'UserName'=>'bjsqq',
            'UserPassword'=>md5('bjsqq#qq202366!')
        ];
    }

    /**
     * 订单列表
     * @param array $param
     * @return mixed
     */
    public function lists($param = []){

        $this->param['RequestBody'] = [
            'OrderBeginDateTime'=>'2022-01-01',
            'OrderEndDateTime'=>'2025-01-01',
        ];


        return $this->curl('OpenIssueOrderList.ashx',$this->param);
    }

    /**
     * 订单列表
     * @param array $param
     * @return mixed
     */
    public function booking2List($param = []){

        $this->booking2['RequestBody'] = [
            'OrderBeginDateTime'=>'2022-01-01',
            'OrderEndDateTime'=>'2025-01-01',
        ];


        return $this->curl('OpenIssueOrderList.ashx',$this->booking2);
    }

    /**
     * 查询订单状态接口
     * @param string $orderNo
     * @return mixed
     */
    public function status($date,$orderNo = ''){

        $this->param['RequestBody'] = [
            'OrderBeginDateTime'=>$date.' 00:00:00',
            'OrderEndDateTime'=>$date.' 23:59:59',
        ];

        if ($orderNo) {

            $this->param['RequestBody']['IssueBillID'] = $orderNo;
        }

        return $this->curl('SearchIssueBill.ashx',$this->param);
    }

    /**
     * 获取订单详情
     * @param $orderNo
     * @return mixed
     */
    public function orderInfo($orderNo)
    {
        $this->param['RequestBody'] = [
            'IssueBillID'=>$orderNo,
        ];

        return $this->curl('IssueBillInfo.ashx',$this->param);
    }

    /**
     * 获取订单详情
     * @param $orderNo
     * @return mixed
     */
    public function booking2OrderInfo($orderNo)
    {
        $this->booking2['RequestBody'] = [
            'IssueBillID'=>$orderNo,
        ];

        return $this->curl('IssueBillInfo.ashx',$this->booking2);
    }

    /**
     * curl请求
     * @param $url
     * @param $param
     * @return mixed
     */
    private function curl($url,$param){

        try {

            $res = CurlService::post($this->http.$url,$param);
            return json_decode(gzdecode(base64_decode($res['result'])),true);

        } catch (\Exception $exception) {


        }

    }


}